oslab3 Linux 动态模块与设备驱动

Linux 动态模块

首先使用grep "sys_call_table" /boot/System.map-$(uname -r)查询sys_call_table的地址
file-20251202210446736.png
然后填入实验指导书提供的示例代码中
另外使用grep -r "gettimeofday" /usr/include/asm-generic/查询,在该服务器上gettimeofday的调用号应该为169
file-20251202211308808.png
修改后发现正确调用该函数
file-20251202224022112.png
下面编写modify_syscall.c

#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/kallsyms.h> 
#include <linux/mm.h>       
#include <asm/pgtable.h>    

//original,syscall 78 function: gettimeofday
// new syscall 78 function: print "No 78 syscall has changed to hello"and return a+b

#define sys_No 169
unsigned long old_sys_call_func;
unsigned long p_sys_call_table=0xffff000008af0698; // find in /boot/System.map-'uname -r'

static void (*update_mapping_prot_func)(phys_addr_t phys, unsigned long virt, phys_addr_t size, pgprot_t prot);
static unsigned long start_rodata;
static unsigned long init_begin;

asmlinkage long hello(const struct pt_regs *regs) //new function
{
    long a = regs->regs[0];
    long b = regs->regs[1];
    printk("No 169 syscall has changed to hello");
    return a + b;
}

static void set_memory_rw(void)
{
    unsigned long size = init_begin - start_rodata;
    update_mapping_prot_func(__pa_symbol(start_rodata), (unsigned long)start_rodata, size, PAGE_KERNEL);
}

static void set_memory_ro(void)
{
    unsigned long size = init_begin - start_rodata;
    update_mapping_prot_func(__pa_symbol(start_rodata), (unsigned long)start_rodata, size, PAGE_KERNEL_RO);
}

void modify_syscall(void)
{
	unsigned long *sys_call_addr;   
    sys_call_addr = (unsigned long *)(p_sys_call_table + sys_No * sizeof(void *));
    old_sys_call_func = *(sys_call_addr);
    set_memory_rw();
    *(sys_call_addr) = (unsigned long)&hello; // point to new function
    set_memory_ro();
}

void restore_syscall(void)
{
    unsigned long *sys_call_addr;
    sys_call_addr = (unsigned long *)(p_sys_call_table + sys_No * sizeof(void *));
    set_memory_rw();
    *(sys_call_addr) = old_sys_call_func; // point to original function
    set_memory_ro();

}

static int mymodule_init(void)
{
    update_mapping_prot_func = (void *)kallsyms_lookup_name("update_mapping_prot");
    start_rodata = kallsyms_lookup_name("__start_rodata");
    init_begin = kallsyms_lookup_name("__init_begin");
    modify_syscall();
    return 0;
}

static void mymodule_exit(void)
{
    restore_syscall();
}

module_init(mymodule_init);
module_exit(mymodule_exit);
MODULE_LICENSE("GPL");

可以看到与指导书中的代码有些区别,修改主要如下:

  1. hello()函数。因为在x86架构的系统中,参数通过栈传递,所以可以像指导书中那样写,但是该华为云的服务器是aarch64架构,参数通过寄存器传递,值pt_regs结构体中。例如:如果是按照指导书中写,运行结果如下 file-20251203002753329.png结果明显不对,再修改为上面的写法后就正确了 file-20251203002852723.png
  2. 修改只读区为可写,写入后再改为只读。经过查询资料Linux ARM64 update_mapping_prot函数_arm架构 怎么 update-CSDN博客,使用update_mapping_prot函数,在对内核地址进行修改前将只读区修改为可写,修改之后再改回只读。
    最终运行结果如下:
    file-20251203012101280.png file-20251203012435899.png
    相关指令:
    make insmod modify_syscall.ko rmmod modify_syscall dmesg | tail

Linux设备驱动

本小节要求实现一个简单的字符设备驱动程序,内核空间模拟字符设备,完成对该设备的打开,读写和释放操作,并编写聊天程序实现对该设备的同步和互斥操作。
根据指导书,实现代码如下:

#include<linux/module.h>
#include<linux/init.h>
#include<linux/fs.h>
#include<asm/uaccess.h>
#include<linux/wait.h>
#include<linux/semaphore.h>
#include <linux/sched.h>
#include <linux/cdev.h>
#include <linux/types.h>
#include<linux/kdev_t.h>
#include <linux/device.h>
#include <linux/uaccess.h>

#define MAXNUM 100
#define MAJOR_NUM 290 //主设备号
#define DEVICE_NAME "globalvar" 
struct Scull_Dev{
    struct cdev devm;        // 字符设备
    struct semaphore sem;    // 实现PV 
    wait_queue_head_t outq;  // 等待队列
    int flag;                // 阻塞唤醒标志
    char buffer[MAXNUM + 1]; // 字符缓冲区
    char *rd, *wr, *end;     // 读,写,尾指针
};

struct Scull_Dev globalvar;
// static struct class *my_class;
int major=MAJOR_NUM;

static ssize_t globalvar_read(struct file *,char *,size_t ,loff_t *);
static ssize_t globalvar_write(struct file *,const char *,size_t ,loff_t *);
static int globalvar_open(struct inode *inode,struct file *filp);
static int globalvar_release(struct inode *inode,struct file *filp);

struct file_operations globalvar_fops =
{
    .read = globalvar_read,//读操作
    .write = globalvar_write,//写操作
    .open = globalvar_open,//打开设备
    .release = globalvar_release,//关闭
};

static int globalvar_init(void)//初始化,insmod时执行
{
    int result = 0;
    //注册字符设备
    result = register_chrdev(MAJOR_NUM, DEVICE_NAME, &globalvar_fops);
    if (result < 0) {
        printk(KERN_ERR "Failed to register character device\n");//注册设备失败
        return result;
    }
    // 初始化同步与缓冲
    sema_init(&globalvar.sem, 1);
    init_waitqueue_head(&globalvar.outq);
    globalvar.rd  = globalvar.buffer;
    globalvar.wr  = globalvar.buffer;
    globalvar.end = globalvar.buffer + MAXNUM;
    globalvar.flag = 0;
    printk("Device initialized successfully\n");//注册设备成功
    return 0;
}

static void globalvar_exit(void)
{
    unregister_chrdev(MAJOR_NUM, DEVICE_NAME);
    printk("Device unregistered successfully\n");//注销设备成功
}

static int globalvar_open(struct inode *inode,struct file *filp)
{
    try_module_get(THIS_MODULE); // 模块计数加一
    printk("This chrdev is in open\n");
    return (0);
}

static int globalvar_release(struct inode *inode,struct file *filp)
{
    module_put(THIS_MODULE); // 模块计数减一
    printk("This chrdev is in release\n");
    return (0);
}

static ssize_t globalvar_read(struct file *filp,char *buf,size_t len,loff_t *off)
{
    if(wait_event_interruptible(globalvar.outq,globalvar.flag!=0)) //不可读时 阻塞读进程
    {
        return -ERESTARTSYS;
    }
    if(down_interruptible(&globalvar.sem)) //P 操作
    {
        return -ERESTARTSYS;
    }
    globalvar.flag = 0;
    printk("into the read function\n");
    printk("the rd is %c\n", *globalvar.rd); // 读指针
    if (globalvar.rd < globalvar.wr)
        len = min(len, (size_t)(globalvar.wr - globalvar.rd)); // 更新读写长度
    else
        len = min(len, (size_t)(globalvar.end - globalvar.rd));
    printk("the len is %ld\n", len);
    if (copy_to_user(buf, globalvar.rd, len))
    {
        printk(KERN_ALERT "copy failed\n");
        up(&globalvar.sem);
        return -EFAULT;
    }
    printk("the read buffer is %s\n",globalvar.buffer);
    globalvar.rd = globalvar.rd + len;
    if(globalvar.rd == globalvar.end)
        globalvar.rd = globalvar.buffer; //字符缓冲区循环
    up(&globalvar.sem); //V 操作
    return len;
}

static ssize_t globalvar_write(struct file *filp,const char *buf,size_t len,loff_t *off)
{
    if(down_interruptible(&globalvar.sem)) //P 操作
    {
        return -ERESTARTSYS;
    }
    if(globalvar.rd <= globalvar.wr)
        len = min(len,(size_t)(globalvar.end - globalvar.wr));
    else
        len = min(len,(size_t)(globalvar.rd-globalvar.wr-1));
    printk("the write len is %ld\n",len);
    if(copy_from_user(globalvar.wr,buf,len))
    {
        up(&globalvar.sem); //V 操作
        return -EFAULT;
    }
    printk("the write buffer is %s\n", globalvar.buffer);
    printk("the len of buffer is %ld\n", strlen(globalvar.buffer));
    globalvar.wr = globalvar.wr + len;
    if (globalvar.wr == globalvar.end)
        globalvar.wr = globalvar.buffer;    // 循环
    up(&globalvar.sem);                     // V 操作
    globalvar.flag = 1;                     // 条件成立,可以唤醒读进程
    wake_up_interruptible(&globalvar.outq); // 唤醒读进程
    return len;
}

module_init(globalvar_init);
module_exit(globalvar_exit);
MODULE_LICENSE("GPL");

使用指令创建文件并给予权限:
file-20251203124018374.png
现在进行简单测试:
file-20251203123847584.png
然后再测试不同进程之间:
file-20251203124231241.png
使用test程序测试:
file-20251203144112349.png
以上代码已经基本实现了不同进程之间的消息交换。